package controller.Supper;

import com.alibaba.fastjson.JSON;
import pojo.BigClass;
import pojo.Smallclass;
import service.Supper.SuperBigService;
import service.Supper.SuperSmallService;
import service.Supper.impl.SuperBigServiceImpl;
import service.Supper.impl.SuperSmallServiceImpl;
import util.PageSupport;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/doSmall")
public class SmallController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        SuperBigService sbs = new SuperBigServiceImpl();
        SuperSmallService sms = new SuperSmallServiceImpl();
        PrintWriter out = response.getWriter();
        String action = request.getParameter("action");
        int pageSize = 5;
        if(action==null){
            action="smallQuery";
        }
        if("smallQuery".equals(action)){   //进入小商品查询
            response.sendRedirect("/page/smallclass/SmallQuery.jsp");
        }else if("selSmallBigName".equals(action)){   //查询大分类下拉列表
            List<BigClass> bigClasses = sbs.selSbig();
            String listJson = JSON.toJSONString(bigClasses);
            out.write(listJson);
        }else if("smallQueryLike".equals(action)){   //分页显示数据
            PageSupport pageSupport = new PageSupport();
            int currentPage = Integer.valueOf(request.getParameter("currentPage"));
            String smallName = request.getParameter("smallName");
            Long  bigName =Long.valueOf(request.getParameter("bigName"));
            int totalCount = sms.getTotalCount(smallName,bigName);   //得到总数据行
            pageSupport.setCurrentPage(currentPage);
            pageSupport.setPageSize(pageSize);
            pageSupport.setTotalCount(totalCount);
            request.getSession().setAttribute("allPags",pageSupport.getTotalPages());
            List<Smallclass> newsList = sms.selSsmlr(currentPage,pageSize,smallName,bigName);
            pageSupport.setList(newsList);
            String jsonList = JSON.toJSONString(pageSupport);
            out.print(jsonList);
        }else if("delSsmall".equals(action)){   //删除
            Long id = Long.valueOf(request.getParameter("id"));
            int n = sms.delSsml(id);
            boolean flag = false;
            if(n>0){
                flag = true;
            }
            String result="{\"flag\":\""+flag+"\"}";
            out.write(result);
        }else if("selById".equals(action)){   //根据id进行查询
            Long id = Long.valueOf(request.getParameter("id"));
            List<Smallclass> smsClasses = sms.selSsmlById(id);
            String listJson = JSON.toJSONString(smsClasses);
            out.write(listJson);
        }else if("smsUpdate".equals(action)){   //修改
            Smallclass smallclass = new Smallclass();
            Long id = Long.valueOf(request.getParameter("id"));
            String smallName = request.getParameter("smallName");
            Long smallBigId = Long.valueOf(request.getParameter("smallBigId"));
            String smallText = request.getParameter("smallText");
            smallclass.setSmallId(id);
            smallclass.setSmallText(smallText);
            smallclass.setSmallName(smallName);
            smallclass.setSmallBigId(smallBigId);
            int n = sms.updateSsml(smallclass);
            boolean flag = false;
            if(n>0){
                flag = true;
            }
            String result="{\"flag\":\""+flag+"\"}";
            out.write(result);
        }else if("addSmall".equals(action)){   //添加
            Smallclass smallclass = new Smallclass();
            smallclass.setSmallName(request.getParameter("smallName"));
            smallclass.setSmallBigId(Long.valueOf(request.getParameter("smallBigId")));
            smallclass.setSmallText(request.getParameter("smallText"));
            int n = sms.addSsml(smallclass);
            boolean flag = false;
            if(n>0){
                flag = true;
            }
            String result="{\"flag\":\""+flag+"\"}";
            out.write(result);
        }else if("checkSamllName".equals(action)){   //验证是否存在相同的小分类昵称
            String smallName = request.getParameter("smallName");
            int n = sms.checkSmallName(smallName);
            boolean flag = false;
            if(n>0){
                flag=true;
            }
            String result="{\"flag\":\""+flag+"\"}";
            out.write(result);
        }
        out.flush();
        out.close();
    }
}
